package com.dj.manager.web;


import com.dj.manager.service.UserService;
import com.dj.pojo.User;
import com.dj.pojo.dto.Pagep;
import com.dj.pojo.dto.UserQuery;
import com.dj.pojo.dto.UserResult;
import com.sun.xml.internal.xsom.impl.scd.Iterators;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;


@Controller
public class UserAction {
    @Autowired
    private UserService userService;
    @ResponseBody
    @RequestMapping(value = "/user",method = RequestMethod.GET)
    public UserResult<User> listUser(Pagep pagep, UserQuery userQuery){
        UserResult<User> result = null;
        try {
            result = userService.listUser(pagep,userQuery);
        }catch(Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("/item/del")
    public boolean deleteId(Long uid){
        boolean d = userService.deleteId(uid);
        return d;
    }




/*   @RequestMapping("/user/editTest")
    public String test(HttpServletRequest request, User user, Map<Object,Integer> map,long uid,String uemail,Long utel,String sex,String uname){

        request.setAttribute("uname",uname);
        request.setAttribute("utel",utel);
        request.setAttribute("uemail",uemail);
        request.setAttribute("sex",sex);
        return "user/add";
    }*/


    @RequestMapping("/item/edit2")
    @ResponseBody
    public User editReview(Long uid){
        System.out.println(uid);

        User user = null;
        try {
            user = userService.selectProductByPid(uid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(user);
        return user;
    }



    @RequestMapping("/user/update")
    @ResponseBody
    public Map<String,Integer> updateUser(HttpServletRequest request){
        Map<String,Integer> map = new HashMap<>();
        Map<String, String[]> parameterMap = request.getParameterMap();
        User user = new User();
        Integer i= 0;
        try {
           BeanUtils.populate(user,parameterMap);
           System.out.println(user);
            i = userService.updateUser(user);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        map.put("i",i);
        return map;
    }

    @RequestMapping("/user/insert")
    @ResponseBody
    public Map<String,Integer> insertUser(HttpServletRequest request) {
        Map<String,Integer> map = new HashMap<>();
        Map<String, String[]> map1 = request.getParameterMap();
        User user = new User();
        Integer d=0;
        try {
            BeanUtils.populate(user,map1);
            d = userService.insertUser(user);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        map.put("d",d);
        return  map;
    }
}
